<?php $this->load->view('templates/backend/header'); ?>
<h3><?php echo $this->lang->line("surplus_list");?></h3>

<?php if (isset($flash_message)  && $flash_message!=NULL ): ?>
<div class="alert alert-success"><a class="close" customerAssess-dismiss="alert">x</a><strong><?php echo $flash_message; ?></strong></div>
<?php endif; ?>
<div class="well">
    <?php echo form_open();?>
        <div class="tab-content">	
            <div class="tab-pane active" id="main">
                <?php
                //ma kh
                echo form_label($this->lang->line('customerID'), 'customerID');
                echo form_input(array('name' => 'customerID', 'id' => 'customerID', 'value' => (isset($surplus->customerID))?$surplus->customerID:'','required'=>'required'));
                echo form_error('customerID');
                //số dư hiện tại
                echo form_label($this->lang->line('surplusNow'), 'surplusNow_fake');
                echo form_input(array('readonly'=>'true','name' => 'surplusNow_fake', 'id' => 'surplusNow_fake', 'value' => ''));
                echo form_input(array('type'=>'hidden', 'name' => 'surplusNow', 'id' => 'surplusNow', 'value' => ''));
                echo form_error('surplusNow_fake');
                //khấu trừ số dư
                echo form_label($this->lang->line('deduct'), 'deduct_fake');
                echo form_input(array('name' => 'deduct_fake', 'id' => 'deduct_fake', 'value' => '','required'=>'required'));
                echo form_input(array('type'=>'hidden', 'name' => 'deduct', 'id' => 'deduct', 'value' => ''));
                echo form_error('deduct_fake');
                //số dư còn lại
                echo form_label($this->lang->line('surplusRemaine'), 'surplusRemaine_fake');
                echo form_input(array('name' => 'surplusRemaine_fake', 'id' => 'surplusRemaine_fake', 'value' => ''));
                echo form_input(array('type'=>'hidden', 'name' => 'surplusRemaine', 'id' => 'surplusRemaine', 'value' => ''));
                echo form_error('surplusRemaine_fake');
                //Ghi chú
                echo form_label($this->lang->line('note'), 'note');
                echo form_textarea(array('name' => 'note', 'id' => 'note', 'value' => (isset($surplus->note))?$surplus->note:'','rows'=>'5','required'=>'required'));
                echo form_error('note');
                ?>
            </div>
        </div>
        <div class="top-button">
                <?php echo form_button(array('id' => 'submit', 'value' => 'Update', 'name' => 'submit', 'type' => 'submit', 'content' => $this->lang->line('save'),'class' => 'btn btn-primary')); ?>
                <a href="<?php echo site_url('auth/surplus'); ?>" class="btn btn-danger"><?php echo $this->lang->line('cancel')?></a>
        </div>
    <?php form_close();?>
    <span id="simple-msg-submit"></span>
</div>

<?php $this->load->view('templates/backend/footer'); ?>
<script src="<?php echo base_url().'static/templates/backend/js/autoNumeric.js';?>"></script>
<script>
$(document).ready(function() {
    $('button[type="submit"]').attr('disabled','disabled');
    $("#customerID").on('blur',function(e) {
            var customerID = $("#customerID").val().trim();
            if(customerID == ""){
                alert("<?php echo $this->lang->line('customerID').' '.$this->lang->line('require');?>");$("#customerID").focus();return false;
            } else {
                setTimeout(function (e) {
                    $.ajax(
                        {
                            url : '/auth/surplus/getContactsById',
                            type: "POST",
                            data : { customerID: customerID},
                            cache: true,
                            beforeSend: function(){

                            },
                            success:function(responseData) 
                            {
                                if(responseData == "-1"){
                                    alert("<?php echo $this->lang->line('customerID').' '.$this->lang->line('not_exists');?>");$("#customerID").focus();return false;
                                } else {
                                    document.getElementById("surplusNow").value                   = UnFormatNumber(responseData);
                                    document.getElementById("surplusNow_fake").value              = FormatNumber(responseData);
                                }
                            },
                            error: function() 
                            {

                            }
                        });
                }, 1000);
                e.preventDefault();	//STOP default action
            }
    });
    
    $('#deduct_fake').autoNumeric('init',{aSign:' VNĐ',mDec:0, pSign:'s' });
    $("#deduct_fake").blur(function() {
        document.getElementById("deduct").value = UnFormatNumber($('#deduct_fake').val());
    });
    
    $('#surplusRemaine_fake').autoNumeric('init',{aSign:' VNĐ',mDec:0, pSign:'s' });
    $("#deduct_fake").blur(function() {
        document.getElementById("surplusRemaine").value         = Number($('#surplusNow').val()) + Number($('#deduct').val());//surplusNow
        document.getElementById("surplusRemaine_fake").value    = FormatNumber($('#surplusRemaine').val());//surplusNow
        
        $('button[type="submit"]').removeAttr('disabled');
    });
	
    $("#surplusRemaine_fake").blur(function() {
		document.getElementById("surplusRemaine").value    = UnFormatNumber($('#surplusRemaine_fake').val());//surplusNow
    });
    
    function FormatNumber(x) {
        if (typeof x === "undefined") {
            return '';
        } else {
        return x.toString().replace(/\B(?=(\d{3})+(?!\d))/g, ",") + ' VNĐ';
        }
    };
    ///,|VNĐ|\s/g
    function UnFormatNumber(x) {
        if (typeof x === "undefined") {
            return '';
        } else {
        return x.toString().replace(/,|VNĐ|\s/g, "");
        }
    };
});
</script>